package sunhang3.parse;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private ParseH264 mParseH264_0;
    private ParseH264 mParseH264_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 正常的
        mParseH264_0 = new ParseH264(this, "vid.h264");
        SurfaceView sv0 = findViewById(R.id.surfaceview0);
        Button btn0 = findViewById(R.id.btn0);
        sv0.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                mParseH264_0.bindSurface(surfaceHolder.getSurface());
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                mParseH264_0.bindSurface(surfaceHolder.getSurface());
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

            }
        });
        btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn0.setEnabled(false);
                mParseH264_0.startWork(new ParseH264.Finished() {
                    @Override
                    public void onFinish() {
                        btn0.setEnabled(true);
                    }
                });
            }
        });


        // 不正常的
        mParseH264_1 = new ParseH264(this, "out.h264");
        SurfaceView sv1 = findViewById(R.id.surfaceview1);
        Button btn1 = findViewById(R.id.btn1);
        sv1.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                mParseH264_1.bindSurface(surfaceHolder.getSurface());
            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                mParseH264_1.bindSurface(surfaceHolder.getSurface());
            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn1.setEnabled(false);
                mParseH264_1.startWork(new ParseH264.Finished() {
                    @Override
                    public void onFinish() {
                        btn1.setEnabled(true);
                    }
                });
            }
        });
    }
}